package com.zhou.nio.client;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @descrition: TCP
 * @author: zhouyongjun
 * @date: 2022/8/29
 **/
public class TcpSocketChannelClient {
    public static void startClient() throws IOException {
        //1 获取通道
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1",8080));
        //2 切换成非阻塞模式
        socketChannel.configureBlocking(false);
        //3 分配指定小的缓存区
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        //4 发送到服务器
        buffer.put(("hello world-").getBytes());
        buffer.flip();
        socketChannel.write(buffer);
        buffer.clear();
        socketChannel.shutdownOutput();
        socketChannel.close();

    }

    public static void main(String[] args) throws IOException {

        startClient();
    }
}
